package com.asset.inventory.viewModel

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.asset.inventory.base.BaseApp
import com.asset.inventory.base.BaseViewModel
import com.asset.inventory.constant.InventoryState
import com.asset.inventory.model.*
import com.asset.inventory.network.HttpUtils
import com.asset.inventory.network.RequestObserver
import com.asset.inventory.network.RxScheduler
import com.asset.inventory.utils.FileUtils
import com.google.gson.Gson

/**
 *    @author : sibb
 *    date   : 21-4-9
 *    desc   : 修改资产配置
 */
public class ModifyAssetsViewModel: BaseViewModel() {

    val userName = ObservableField<String>()

    val userDepartment = ObservableField<String>()

    val location = ObservableField<String>()

    var detailBean: TaskDetailBean.Detail? = null

    val isModifySuccess = MutableLiveData<Boolean>()

    var mTaskId = 0L

    private val isShowLoading = MutableLiveData<Boolean>()


    fun loadingLiveData(): MutableLiveData<Boolean> = isShowLoading

    fun setTaskId(id: Long?){
        if(id != null){
            mTaskId = id
        }

    }

    fun observerModifyStatus() = isModifySuccess

    fun bindingData(data: TaskDetailBean.Detail?){
        data?.let {
            detailBean = it
        }
    }

    fun confirmModify(departmentInfo: ConfigInfo?,userInfo: ConfigInfo?,locationInfo: ConfigInfo?){
        isShowLoading.postValue(true)
        detailBean?.let {
            it.dutyResultStatus = InventoryState.STATE_EQUALITY
            it.updateOrgName = departmentInfo?.name
            it.updateOrgCode = departmentInfo?.code
            it.updateUserId = userInfo?.code
            it.updateUserName = userInfo?.name
            it.updateLocationCode = locationInfo?.code
            it.updateLocationName = locationInfo?.name
            if(BaseApp.instance.isOnlineModel){
                HttpUtils.instance.putAssetsField(it.id,it).compose(RxScheduler.io_main()).subscribe(object: RequestObserver<ModifyAssetsModel>{
                    override fun onSuccess(t: ModifyAssetsModel) {
                        if(t.status == "success"){
                            isModifySuccess.postValue(true)
                        }else{
                            isModifySuccess.postValue(false)
                        }
                        isShowLoading.postValue(false)
                    }

                    override fun onFaile(e: Throwable) {
                        isModifySuccess.postValue(false)
                        isShowLoading.postValue(false)
                    }
                })
            }else{
                var waitCount = 0
                var finishCount = 0
                val fileContent = FileUtils.getContentFromSdcard()
                val offlineModel =  Gson().fromJson(fileContent, OfflineModel::class.java)
                if(offlineModel?.data != null){
                   for(task in offlineModel.data){
                        if(task.id == mTaskId){
                            val details = task.dutyResults
                            if(details != null){
                                for(detail in details){
                                    if(detail.id == detailBean?.id){
                                        detail.dutyResultStatus = InventoryState.STATE_EQUALITY
                                        detail.updateOrgName = departmentInfo?.name
                                        detail.updateOrgCode = departmentInfo?.code
                                        detail.updateUserId = userInfo?.code
                                        detail.updateUserName = userInfo?.name
                                        detail.updateLocationCode = locationInfo?.code
                                        detail.updateLocationName = locationInfo?.name
                                    }
                                    if(detail.dutyResultStatus == InventoryState.STATE_WAIT){
                                        waitCount++
                                    }else if(detail.dutyResultStatus == InventoryState.STATE_EQUALITY || detail.dutyResultStatus == InventoryState.STATE_LOSS){
                                        finishCount++
                                    }
                                }
                            }
                            task.dutyResultDoneCount = finishCount
                            task.dutyResultWaitCount = waitCount

                        }
                   }
                }
                val modifyData = Gson().toJson(offlineModel)
                FileUtils.write2File(modifyData)
                isModifySuccess.postValue(true)
                isShowLoading.postValue(false)
            }
        }


    }



}